package com.sdy.dcsb.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdy.common.model.Response;
import com.sdy.dcsb.biz.model.EsbServiceConsumer;
import com.sdy.dcsb.biz.model.EsbServiceConsumerJoint;
import com.sdy.dcsb.biz.service.EsbServiceConsumerService;
import com.sdy.dcsb.biz.service.UserOperationLogService;
import com.sdy.mvc.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;
import static org.springframework.util.StringUtils.isEmpty;

/**
 * <p>
 * IP授权记录信息表 前端控制器
 * </p>
 *
 * @author fb
 * @since 2019-09-29
 */
@Slf4j
@Controller
@RequestMapping("/EsbServiceConsumer")
@Api(tags = "服务的消费者ip信息权限控制相关接口")
public class EsbServiceConsumerController extends BaseController {
    @Autowired
    private EsbServiceConsumerService esbServiceConsumerService;
    @Autowired
    private UserOperationLogService userOperationLogService;

    /**
     * 查询服务的消费者ip信息
     * @param serviceid ,当前服务id
     * @param ip ,ip地址
     * @param current ,当前页
     * @param size ,每页数量
     * @return
     */
    @GetMapping("/select")
    @ResponseBody
    @ApiOperation("查询服务的消费者ip信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "serviceid", value = "当前服务id"),
            @ApiImplicitParam(dataType = "String", name = "ip", value = "ip地址"),
            @ApiImplicitParam(dataType = "Integer", name = "current", value = "当前页"),
            @ApiImplicitParam(dataType = "Integer", name = "size", value = "每页数量"),
    })
    public Response select(Integer serviceid, String ip, Integer current, Integer size) {
        if(isEmpty(serviceid)) {
            return Response.error("请求的serviceid不能为空");
        }
        try {
            if(!isEmpty(ip)) {
                ip = URLDecoder.decode(ip,"utf-8");
            }
        }catch(IOException e) {

        }
        JSONObject object = new JSONObject();
        if(isEmpty(current)) {
            current = 1;
        }
        if(isEmpty(size)) {
            size = 10;
        }
        object.put("size",size);
        object.put("current",current);
        object.put("searchCount",true);

        Integer total = 0;
        Integer page = 0;

        List<EsbServiceConsumerJoint> resultCount = esbServiceConsumerService.findServiceConsumerIpCount(serviceid, ip,"");

        total = resultCount.size();
        if(total%size==0){
            page = total/size;
        }else {
            page = total/size+1;
        }

        List<EsbServiceConsumerJoint> result = esbServiceConsumerService.findServiceConsumerIp(serviceid, ip, "", current, size);
        object.put("records",result);
        object.put("total",total);
        object.put("pages",page);

        return Response.success(object);
    }

    /**
     * 查询服务清单：单条服务的消费者ip信息
     * @param serviceid ,当前服务id
     * @param ip ,ip地址
     * @param current ,当前页
     * @param size ,每页数量
     * @return
     */
    @GetMapping("/selectServiceList")
    @ResponseBody
    @ApiOperation("查询服务清单：单条服务的消费者ip信息")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "serviceid", value = "当前服务id"),
            @ApiImplicitParam(dataType = "String", name = "ip", value = "ip地址"),
            @ApiImplicitParam(dataType = "Integer", name = "current", value = "当前页"),
            @ApiImplicitParam(dataType = "Integer", name = "size", value = "每页数量"),
    })
    public Response selectServiceList(Integer serviceid, String ip, Integer current, Integer size) {
        if(isEmpty(serviceid)) {
            return Response.error("请求的serviceid不能为空");
        }
        try {
            if(!isEmpty(ip)) {
                ip = URLDecoder.decode(ip,"utf-8");
            }
        }catch(IOException e) {

        }

        JSONObject object = new JSONObject();
        if(isEmpty(current)) {
            current = 1;
        }
        if(isEmpty(size)) {
            size = 10;
        }
        object.put("size",size);
        object.put("current",current);
        object.put("searchCount",true);

        Integer total = 0;
        Integer page = 0;

        List<EsbServiceConsumerJoint> resultCount = esbServiceConsumerService.findServiceConsumerIpCount(serviceid, ip,"1");

        total = resultCount.size();
        if(total%size==0){
            page = total/size;
        }else {
            page = total/size+1;
        }

        List<EsbServiceConsumerJoint> result = esbServiceConsumerService.findServiceConsumerIp(serviceid, ip, "1", current, size);
        object.put("records",result);
        object.put("total",total);
        object.put("pages",page);

        return Response.success(object);
    }

    /**
     * 修改当前服务下消费者ip授权信息，是否有权限
     * @param id ,当前流水id,EsbServiceConsumer的id
     * @param status ,状态。1:有权限,0:无权限
     * @return
     */
    @GetMapping("/update")
    @ResponseBody
    @ApiOperation("修改当前服务下消费者ip授权信息，是否有权限")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "Integer", name = "id", value = "当前流水id,EsbServiceConsumer的id"),
            @ApiImplicitParam(dataType = "String", name = "status", value = "状态。1:有权限,0:无权限"),
    })
    public Response update(HttpServletRequest request, Integer id, String status) {
        if(isEmpty(id)) {
            return Response.error("请求的id不能为空");
        }
        if(!isEmpty(status)) {
            try {
                status = URLDecoder.decode(status,"utf-8");
            }catch(IOException e) {

            }
            EsbServiceConsumer esbServiceConsumer = new EsbServiceConsumer();
            esbServiceConsumer.setStatus(status);
            esbServiceConsumer.setUpdateTime(new Date());
            userOperationLogService.savaUserLog(request, "修改服务的消费者ip权限：" +
                    esbServiceConsumerService.getById(id).getServcode());
            return Response.success(esbServiceConsumerService.update(esbServiceConsumer, new UpdateWrapper<EsbServiceConsumer>().eq("id", id)));
        }else {
            return Response.error("授权状态不能为空");
        }
    }

}
